//
//  LoginViewController.m
//  Recycling
//
//  Created by jzd on 2018/5/16.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "LoginViewController.h"
#import "SJTabbarController.h"
#import "UserInfo.h"


@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
@property (weak, nonatomic) IBOutlet UIButton *loginBut;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BGCOLOR;
    self.topHeightCons.constant = statsBarHeight+NavgationBarH;
    self.topCons.constant = statsBarHeight;

    [self.phone addTarget:self action:@selector(phoneChange:) forControlEvents:UIControlEventEditingChanged];
    [self.pwd addTarget:self action:@selector(pwdChange:) forControlEvents:UIControlEventEditingChanged];
    self.phone.delegate = self;
    self.pwd.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.phone becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [[SJNetWorkTool sharedTool].tasks makeObjectsPerformSelector:@selector(cancel)];
}
-(void)phoneChange:(UITextField *)textV{
    if (textV.text.length == 0) {
        self.loginBut.enabled = NO;
    }else{
        if (self.pwd.text.length == 0) {
            self.loginBut.enabled = NO;
        }else{
            self.loginBut.enabled = YES;
        }
    }
}
-(void)pwdChange:(UITextField *)textV{
    if (textV.text.length == 0) {
        self.loginBut.enabled = NO;
    }else{
        if (self.phone.text.length == 0) {
            self.loginBut.enabled = NO;
        }else{
            self.loginBut.enabled = YES;
        }
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (IBAction)loginOnClick:(UIButton *)sender {
    [self.view endEditing:YES];
    NSDictionary *parameters = @{@"loginname":self.phone.text,@"loginpwd":[SJMD5Tool MD5ForLower32Bate:self.pwd.text],@"client_type":@"2"};
    [[SJNetWorkTool sharedTool]loginWithURLString:login parameters:parameters callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            if (code == 1) {
                NSDictionary *dataDic = [responseObject objectForKey:@"Data"];
                UserInfo *user = [[UserInfo alloc]setValueDicts:dataDic];
                user.MD5Pwd = [SJMD5Tool MD5ForLower32Bate:self.pwd.text];
                [NSKeyedArchiver archiveRootObject:user toFile:[UserInfoTool shareUserInfoTool].savePath];
                [UserInfoTool shareUserInfoTool].Info = user;
                
                SJTabbarController *tab = [[SJTabbarController alloc]init];
                [UIApplication sharedApplication].keyWindow.rootViewController = tab;
                //登录成功 注册别名
                if ([[NSUserDefaults standardUserDefaults]objectForKey:DeviceToken]) {
                    if ([[UserInfoTool shareUserInfoTool].Info.Role  isEqualToString:@"030003002"]) {
                        //绑定别名
                        NSString *alias = [NSString stringWithFormat:@"%d",[UserInfoTool shareUserInfoTool].Info.ID];
                        [UMessage addAlias:alias type:@"WORKER_TYPE" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
                            SJLog(@"%@-%@",responseObject,error);
                        }];
                    }
                }
            }
            [self.view showToast:msg completion:nil];
        }
    }];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.phone) {
        self.topView.backgroundColor = TITLECOLOR;
    }else{
        self.bottomView.backgroundColor = TITLECOLOR;
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.phone) {
        self.topView.backgroundColor = [UIColor lightGrayColor];
    }else{
        self.bottomView.backgroundColor = [UIColor lightGrayColor];
    }
}

@end
